#!/usr/bin/env script/runner

config = ActsAsFerret::Remote::Config.load

def send_signal(signal, pid_file)
  pid = open(pid_file).read.to_i
  print "Sending #{signal} to ferret_server with PID #{pid}..."
  begin
    Process.kill(signal, pid)
  rescue Errno::ESRCH
    puts "Process does not exist. Not running. Removing stale pid file anyway."
    File.unlink(pid_file)
  end

  puts "Done."
end

pid_file = config['pid_file']
puts "Stopping ferret_server..."
send_signal("TERM", pid_file)

# vim:set filetype=ruby:
